using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Globalization;
using System.Xml;

namespace LongNet.Config
{
    internal static class ConfigHelper
    {
        internal static string ExpandVariables(string input, Dictionary<string, string> variables)
        {
            if (variables == null || variables.Count == 0)
                return input;

            string output = input;
            // make this case-insensitive, will probably require a different approach
            foreach (string s in variables.Keys)
            {
                output = output.Replace("{" + s + "}", variables[s]);
            }

            return output;
        }

        internal static XmlNode[] GetChildElements(XmlElement element)
        {
            ArrayList list = new ArrayList();
            foreach (XmlNode n in element.ChildNodes)
            {
                if (n is XmlElement)
                    list.Add(n);
            }
            return (XmlElement[])list.ToArray(typeof(XmlElement));
        }

        internal static XmlNode[] GetChildElements(XmlElement element, string localName)
        {
            ArrayList list = new ArrayList();
            foreach (XmlNode n in element.ChildNodes)
            {
                if (0 != String.Compare(localName, n.LocalName, true))
                    continue;

                if (n is XmlElement)
                    list.Add(n);
            }
            return (XmlElement[])list.ToArray(typeof(XmlElement));
        }

        internal static bool CaseIgnoreEquals(string strA, string strB)
        {
            return string.Compare(strA, strB, true) == 0;
        }

        internal static bool HasCaseInsensitiveAttribute(XmlElement element, string name)
        {
            // first try a case-sensitive match
            if (element.HasAttribute(name))
                return true;

            // then look through all attributes and do a case-insensitive compare
            foreach (XmlAttribute a in element.Attributes)
            {
                if (CaseIgnoreEquals(a.LocalName, name))
                    return true;
            }

            return false;
        }

        internal static string GetCaseInsensitiveAttribute(XmlElement element, string name, Dictionary<string, string> variables)
        {
            // first try a case-sensitive match
            string attr = element.GetAttribute(name);
            if (attr != null && attr.Length > 0)
                return ExpandVariables(attr, variables);

            // then look through all attributes and do a case-insensitive compare
            foreach (XmlAttribute xa in element.Attributes)
            {
                if (CaseIgnoreEquals(xa.LocalName, name))
                    return ExpandVariables(xa.Value, variables);
            }

            return null;
        }


        private static object GetEnumValue(Type enumType, string value)
        {
            if (enumType.IsDefined(typeof(FlagsAttribute), false))
            {
                ulong union = 0;

                foreach (string v in value.Split(','))
                {
                    FieldInfo enumField = enumType.GetField(v.Trim(), BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
                    union |= Convert.ToUInt64(enumField.GetValue(null));
                }
                object retval = Convert.ChangeType(union, Enum.GetUnderlyingType(enumType), CultureInfo.InvariantCulture);
                return retval;
            }
            else
            {
                FieldInfo enumField = enumType.GetField(value, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public);
                return enumField.GetValue(null);
            }
        }

        internal static Dictionary<string, TValue> CreateDictionary<TValue>()
        {
            return new Dictionary<string, TValue>(StringComparer.InvariantCultureIgnoreCase);
        }
    }
}
